import { createWebHistory, createRouter, RouteRecordRaw } from "vue-router";
import { useAuthStore } from '@/store/auth';

const routes = [
    {
        path: '',
        redirect: "/home",
    },
    {
        path: '/home',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页', requiresAuth: true }
    },
    {
        name: 'AISearch',
        path: '/home/ai-search',
        component: () => import('@/views/home/ai-search/index.vue'),
        meta: { title: 'AI搜索', requiresAuth: true }
    },
    {
        path: '/assistant',
        component: () => import('@/views/assistant/index.vue'),
        meta: { title: 'AI助手', requiresAuth: true }
    },
    {
        name: 'KnowledgeGraph',
        path: '/knowledge-graph',
        component: () => import('@/views/knowledge-graph/index.vue'),
        meta: { title: '知识图谱', requiresAuth: true }
    },
    {
        name: 'SmartTeaching',
        path: '/smart-teaching',
        redirect: '/smart-teaching/ai-search',
        component: () => import('@/views/smart-teaching/index.vue'),
        children: [
            {
                name: 'SmartTeachingAiSearch',
                path: 'ai-search',
                component: () => import('@/views/smart-teaching/steps/AiSearch.vue'),
                meta: { title: '深度搜索', step: 'ai-search' }
            },
            {
                name: 'SmartTeachingAnalysis',
                path: 'analysis',
                component: () => import('@/views/smart-teaching/steps/Analysis.vue'),
                meta: { title: '深度分析', step: 'analysis' }
            },
            {
                name: 'SmartTeachingChart',
                path: 'chart',
                component: () => import('@/views/smart-teaching/steps/Chart.vue'),
                meta: { title: '一键作图', step: 'chart' }
            },
            {
                name: 'SmartTeachingPPT',
                path: 'ppt',
                component: () => import('@/views/smart-teaching/steps/PPT.vue'),
                meta: { title: '一键做PPT', step: 'ppt' }
            },
            {
                name: 'SmartTeachingQuestion',
                path: 'question',
                component: () => import('@/views/smart-teaching/steps/Question.vue'),
                meta: { title: '一键出题', step: 'question' }
            }
        ]
    },
    {
        path: '/community',
        component: () => import('@/views/community/index.vue'),
        meta: { title: '社区', requiresAuth: true }
    },
    {
        path: '/message',
        component: () => import('@/views/message/index.vue'),
        meta: { title: '消息', requiresAuth: true }
    },
    {
        path: '/my',
        component: () => import('@/views/my/index.vue'),
        meta: { title: '我的', requiresAuth: true }
    },
    {
        path: '/login',
        component: () => import('@/views/my/login/index.vue'),
        meta: { title: '登录', requiresAuth: false }
    },
    {
        path: '/message/search',
        component: () => import('@/views/message/search/index.vue'),
        meta: { title: '搜索', requiresAuth: true }
    },
    {
        name: 'UserIndex',
        path: '/user/index',
        component: () => import('@/views/my/index/index.vue'),
    },
    {
        name: 'MessageChat',
        path: '/message/chat',
        component: () => import('@/views/message/chat/index.vue'),
        props: true
    },
    {
        name: 'OnlineCode',
        path: '/online/code',
        component: () => import('@/views/home/code/index.vue')
    },
    {
        name: 'OnlineCodeCreate',
        path: '/online/code/create',
        component: () => import('@/views/home/code/create/index.vue')
    },
    {
        name: 'UserCodeList',
        path: '/user/code/list',
        component: () => import('@/views/home/code/list/index.vue'),
        meta: { title: '在线编程', requiresAuth: true }
    },
    {
        name: 'UserInfo',
        path: '/user/info',
        component: () => import('@/views/my/info/index.vue'),
    },
    {
        name: 'Nickname',
        path: '/user/info/nickname',
        component: () => import('@/views/my/info/nickname/index.vue'),
        props: true,
    },
    {
        name: 'Setting',
        path: '/setting',
        component: () => import('@/views/my/setting/index.vue'),
        props: true,
    },
    {
        name: 'CommunityCreate',
        path: '/community/create',
        component: () => import('@/views/community/create/index.vue'),
    },
    {
        name: 'CommunityHome',
        path: '/community/home',
        component: () => import('@/views/community/home/index.vue')
    },
    {
        name: 'CommunitySetting',
        path: '/community/setting',
        component: () => import('@/views/community/setting/index.vue')
    },
    {
        name: 'CommunityArticleDetail',
        path: '/community/article/:id',
        component: () => import('@/views/community/article/index.vue'),
        props: true
    },
    {
        name: 'CommunityArticleEdit',
        path: '/community/article/edit',
        component: () => import('@/views/community/article/edit/index.vue')
    },
    {
        name: 'Subject',
        path: '/subject',
        redirect: { name: 'SubjectList' },
        children: [
            {
                name: 'SubjectList',
                path: 'list',
                component: () => import('@/views/home/subject/list/index.vue'),
                meta: { title: '刷题测验', requiresAuth: true }
            },
            {
                name: 'SubjectCreate',
                path: 'create',
                component: () => import('@/views/home/subject/create/index.vue')
            },
            {
                name: 'SubjectHome',
                path: 'home',
                component: () => import('@/views/home/subject/home/index.vue')
            },
            {
                name: 'SubjectManage',
                path: 'manage',
                component: () => import("@/views/home/subject/manage/index.vue")
            },
            {
                name: 'ExerciseData',
                path: 'exercise',
                component: () => import("@/views/home/subject/exercise/index.vue")
            },
            {
                name: 'Question',
                path: 'question',
                children: [
                    {
                        name: 'QuestionAnswer',
                        path: 'answer',
                        component: () => import("@/views/home/subject/question/answer/index.vue")
                    },
                    {
                        name: 'QuestionCreate',
                        path: 'create',
                        component: () => import("@/views/home/subject/question/create/index.vue")
                    }
                ]
            }
        ]
    },
    {
        name: 'Classroom',
        path: '/classroom',
        redirect: { name: 'classroomList' },
        children: [
            {
                name: 'classroomList',
                path: 'list',
                component: () => import('@/views/home/classroom/list/index.vue')
            },
            {
                name: 'coursecreat',
                path: '/coursecreate',
                component: () => import('@/views/home/classroom/create/index.vue'),
            },
            {
                name: 'MangerCourse',
                path: '/MangerCourse',
                component: () => import('@/views/home/classroom/manage/Course.vue'),
            },
            {
                name: 'course',
                path: 'course',
                component: () => import('@/views/home/classroom/course/index.vue'),
            },
            {
                name: 'joinClassroom',
                path: 'join',
                component: () => import('@/views/home/classroom/join/index.vue'),
            },
            {
                name: 'manageStudents',
                path: '/classroom/manage/students',
                component: () => import('@/views/home/classroom/manage/Students.vue'),
            },
            {
                name: 'manageTeachers',
                path: '/classroom/manage/teachers',
                component: () => import('@/views/home/classroom/manage/Teachers.vue'),
            },
            {
                name: 'manageAssignments',
                path: '/classroom/manage/assignments',
                component: () => import('@/views/home/classroom/manage/Assignments.vue'),
            },
            {
                name: 'submissions',
                path: '/classroom/manage/assignments/detail',
                component: () => import('@/views/home/classroom/manage/Submissions.vue'),
            },
            {
                name: 'manageExams',
                path: '/classroom/manage/exams',
                component: () => import('@/views/home/classroom/manage/Exams.vue'),
            },
            {
                name: 'examDetail',
                path: '/classroom/manage/exams/detail',
                component: () => import('@/views/home/classroom/manage/ExamsDetail.vue'),
            },
            {
                name: 'examResults',
                path: '/classroom/manage/exams/results',
                component: () => import('@/views/home/classroom/manage/ExamsResults.vue'),
            },
            {
                name: 'createClassroom',
                path: '/classroom/create',
                component: () => import('@/views/home/classroom/create/index.vue'),
            },
            {
                name: 'createQuestion',
                path: '/classroom/create/question',
                component: () => import('@/views/home/classroom/question/index.vue'),
            }
        ]
    },
    {
        name: 'stuInfo',
        path: '/stuInfo',
        component: () => import('@/views/stu/stuInfo.vue'),
    },
    {
        name: 'Course',
        path: '/course',
        children: [
            {
                name: 'CourseHome',
                path: 'home',
                component: () => import('@/views/home/course/home/index.vue')
            }
        ]
    }
] as Readonly<RouteRecordRaw[]>;

export const router = createRouter({
    history: createWebHistory(),
    routes: routes,
});

const whiteList = ['/login'];

router.beforeEach((to, _from, next) => {
    document.title = to.meta.title ? `${to.meta.title} - WitData` : 'WitData';

    const authStore = useAuthStore();
    
    if (authStore.getIsLogin) {
        if (to.path === '/login') {
            next('/home');
        } else {
            next();
        }
    } else {
        if (whiteList.includes(to.path)) {
            next();
        } else {
            next(`/login?redirect=${to.path}`);
        }
    }
});
